package com.boot.system.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.framework.common.DropList;
import com.boot.framework.constants.PageConstants;
import com.boot.system.dto.SysRoleDTO;
import com.boot.system.entity.SysRoleEntity;
import com.boot.system.mapper.SysRoleMapper;
import com.boot.system.wrapper.SysRoleWrapper;
import com.boot.system.service.ISysRoleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 系统角色
 *
 * @author yangfan
 * @date 2023-12-25 20:56:53
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements ISysRoleService {

    private QueryWrapper<SysRoleEntity> getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<>();
        String roleName = (String) params.get("roleName");
        wrapper.like(StrUtil.isNotEmpty(roleName), "role_name", roleName);
        return wrapper;
    }

    private Page<SysRoleEntity> getPage(Map<String, Object> params) {
        long pageNum = Long.parseLong(String.valueOf(params.get(PageConstants.PAGE)));
        long pageSize = Long.parseLong(String.valueOf(params.get(PageConstants.LIMIT)));
        return new Page<>(pageNum, pageSize);
    }

    @Override
    public IPage<SysRoleDTO> selectPage(Map<String, Object> params) {
        return SysRoleWrapper.build().pageDTO(baseMapper.selectPage(getPage(params), getQueryWrapper(params)));
    }

    @Override
    public List<SysRoleDTO> selectList(Map<String, Object> params) {
        return SysRoleWrapper.build().listDTO(baseMapper.selectList(getQueryWrapper(params)));
    }

    @Override
    public List<DropList> dropList() {
        List<SysRoleEntity> roles = baseMapper.selectList(Wrappers.emptyWrapper());
        List<DropList> dropLists = new ArrayList<>();
        for (SysRoleEntity sysRole : roles) {
            dropLists.add(DropList.builder()
                    .id(sysRole.getRoleId())
                    .label(sysRole.getRoleName())
                    .value(sysRole.getRoleCode()).build());
        }
        return dropLists;
    }

    @Override
    public List<SysRoleDTO> selectByIds(List<String> ids) {
        return SysRoleWrapper.build().listDTO(baseMapper.selectBatchIds(ids));
    }
}